package com.aldobucchi.x.mx.controls.TextArea
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import mx.collections.ArrayCollection;
	import mx.core.mx_internal;
	
	import com.aldobucchi.utils.App;
	
	/**
	 * 
	 * 
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class TextAreaInterceptor
	{
		// todo: finish this list
		private var keysThatDontChangeText:ArrayCollection = new ArrayCollection( [
				Keyboard.UP,
				Keyboard.DOWN,
				Keyboard.LEFT,
				Keyboard.RIGHT
			] );
		
		
		/**
		* target is not typed because it can be a textarea or a textinput
		*/		
		private var target:*;

		private function get textField():TextField
		{
			return target.mx_internal::getTextField();
		}

		private var savedText:String;
		
		private var savedCaretIndex:int;
		
		private var restoreOnBubblingPhaseFlag:Boolean;
		
		private var restoreOnTextChangeFlag:Boolean;

		public function TextAreaInterceptor( target:* )
		{
			this.target = target;
		
		}
		
		public function start():void
		{
			App.app.systemManager.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown, true, 1000 );
			App.app.systemManager.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDownBubbling, false, 0 );
			target.addEventListener( Event.CHANGE, onTextChange );
		}
		
		public function stop():void
		{
			App.app.systemManager.removeEventListener( KeyboardEvent.KEY_DOWN, onKeyDown, true );
			App.app.systemManager.removeEventListener( KeyboardEvent.KEY_DOWN, onKeyDownBubbling, false );
			target.removeEventListener( Event.CHANGE, onTextChange );
		}
		
		private function acceptKeyboardEvent( event:KeyboardEvent ):Boolean
		{
			switch ( event.keyCode )
			{
				case Keyboard.UP:
				case Keyboard.DOWN:
				case Keyboard.ENTER:

					return false;
				break;		
			}
			
			return true;		
		}
		

		
		private function onKeyDown( event:KeyboardEvent ):void
		{
			fyi( "-------------------" );
			fyi( "onKeyDown" );
			debug();
			if ( ! acceptKeyboardEvent( event ) )
			{
				store();
				if ( keysThatDontChangeText.contains( event.keyCode ) )
					restoreOnBubblingPhaseFlag = true;
				else
					restoreOnTextChangeFlag = true;
			}
		}

		private function onKeyDownBubbling( event:KeyboardEvent ):void
		{
			fyi("onKeyDownBubbling");
			debug();	
			if ( restoreOnBubblingPhaseFlag )
				restore();
		}
		
		private function onTextChange( event:Event ):void
		{
			fyi("onTextChange");
			debug();
			if ( restoreOnTextChangeFlag )
				restore();
		}
		
		
		
		private function store():void
		{
			fyi( "store" );
			savedText = target.text;
			savedCaretIndex = textField.caretIndex;		
		}
		
		
		private function restore():void
		{
			fyi("restore");
			target.text = savedText;
			target.setSelection( savedCaretIndex, savedCaretIndex )
			restoreOnBubblingPhaseFlag = false;
			restoreOnTextChangeFlag = false;
		}
		
		
		private function fyi( msg:String ):void
		{
			// trace( msg );
		}
		
		
		private function debug():void
		{
			fyi( "target.text = " + target.text );
			fyi( "target.caretIndex = " + textField.caretIndex );		
		}
		

	}
}